var jsonstr_qc=[{'searchType':'通信状态','typeContent':[{'typeContent':'在线'},{'typeContent':'离线'}]},{'searchType':'负载状态','typeContent':[{'typeContent':'空载'},{'typeContent':'负载'}]},{'searchType':'充电状态','typeContent':[{'typeContent':'充电中'},{'typeContent':'未充电'}]},
    {'searchType':'告警类型','typeContent':[{'typeContent':'车体告警'},{'typeContent':'温湿度告警'},{'typeContent':'通信状态告警'},{'typeContent':'终端故障告警'},{'typeContent':'电子围栏告警'}]}];
var jsonstr_qc2=[];
//测试条件
function test(){
    $.each(jsonstr_qc,function(k,v){
        var tbData = {};   //对象
        tbData.searchType = jsonstr_qc[k].searchType;
        var content = jsonstr_qc[k].typeContent;
        var ctt="";
        $.each(content,function(i,j){
            var text = content[i].typeContent;
            ctt += "<a role='button' class='btn' onclick='add(text)' href='#'>"+content[i].typeContent+"</a>&nbsp;";
        });
        tbData.typeContent = ctt;
        jsonstr_qc2.push(tbData);
    });
}


$(document).ready(function(){
	   initBarChart();
	   initPieChart();
        $('#using_json').jstree({
            'core' : {
            'data' : [               
                {
                    'text': '电动物流车A组',
                    'state': {
                        'opened': true
                    },
                    'children': [
                        {
                            'text': '电动物流车001',                       
                            'state': {
                                'opened': true
                            },
							'numberPlate':'苏A J78921'
                        },
                        {
                            'text': '电动物流车002',                     
                            'state': {
                                'opened': true
                            },
							'numberPlate':'苏A J78922'
                        },
						 {
                            'text': '电动物流车003',                       
                            'state': {
                                'opened': true
                            },
							'numberPlate':'苏A J78923'
                        },
						 {
                            'text': '电动物流车004',                       
                            'state': {
                                'opened': true
                            },
							'numberPlate':'苏A J78924'
                        },
						 {
                            'text': '电动物流车005',                       
                            'state': {
                                'opened': true
                            },
							'numberPlate':'苏A J78925'
                        },                      
                    ]
                },
                {
				   'text':'电动物流车B组',
				   'state': {
                                'opened': true
                            },
				'children': [
                        {
                            'text': '电动物流车006',                       
                            'state': {
                                'opened': true
                            },
							'numberPlate':'苏B J78921'
                        },
						 {
                            'text': '电动物流车007',                       
                            'state': {
                                'opened': true
                            },
							'numberPlate':'苏B J78922'
						 },
			               ],
				},
				
                '电动物流车C组',
                '电动物流车D组',
                '电动物流车E组',
            ]
        } });

       test();
       $('#qc_table').bootstrapTable({
           //url: 'data1.json',
           columns: [{
               field: 'searchType',
               title: '条件类型'
           }, {
               field: 'typeContent',
               title: '条件内容'
           }],
           data:jsonstr_qc2
       });


      window.operateEvents = {
           'click .location' : function(e, value, row, index) {
               window.location.href = "../cljc/clgjjc.html?numberPlate="+encodeURI(row.numberPlate)+"";
           },
           'click .monitor' : function(e, value, row, index) {
               window.location.href = "../cljc/cljc_xq.html?numberPlate="+encodeURI(row.numberPlate)+"";
           }
       }

       function operateFormatter(value,row,index) {
           return ['<a role="button" id="#dw" onclick="temp(this.id);">定位</a>&nbsp;&nbsp;<a role="button" id="#mt" onclick="temp(this.id);">监测</a>'];
       }

		$('#xq_table').bootstrapTable({
			//url: 'data1.json',		  
		   pagination:true,                  
           pageNumber:1,                       //初始化加载第一页，默认第一页
           pageSize: 2,                       //每页的记录行数（*）
           pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
 
		   columns: [{
				field: 'dateTime',
				title: '日期'
			}, {
				field: 'numberPlate',
				title: '车牌号'
			}, {
				field: 'contactState',
				title: '通信状态'
			},{
				field: 'loadState',
				title: '负载状态'
			},{
				field: 'chargeState',
				title: '充电状态'
			},{
				field: 'carAram',
				title: '车体告警'
			},{
				field: 'thAram',
				title: '温湿度告警'
			},{
				field: 'contactAram',
				title: '通信状态告警'
			},{
				field: 'deviceAram',
				title: '设备告警'
			},{
				field: 'fenceAram',
				title: '电子围栏告警'
			},{	
				field : 'operation',
				title : '操作',
				align : 'center',
				events : operateEvents,
				formatter : operateFormatter
			}],
			data: [{
				dateTime: '2017-08-03 14:30:00',
				numberPlate: '苏AH8903',
				contactState: '在线',
				loadState: '负载',
				chargeState: '未充电',
				carAram: '否',
				thAram: '是',
				contactAram: '是',
				deviceAram: '否',
				fenceAram: '否',
				operation: '定位  监测'			   		        
		},
			{
				dateTime: '2017-08-03 14:31:00',
				numberPlate: '苏AH8903',
				contactState: '在线',
				loadState: '负载',
				chargeState: '未充电',
				carAram: '否',
				thAram: '是',
				contactAram: '是',
				deviceAram: '否',
				fenceAram: '否',
				operation: '定位  监测'	
			},
			{
				dateTime: '2017-08-03 14:32:00',
				numberPlate: '苏AH8903',
				contactState: '在线',
				loadState: '负载',
				chargeState: '未充电',
				carAram: '否',
				thAram: '是',
				contactAram: '是',
				deviceAram: '否',
				fenceAram: '否',
				operation: '定位  监测'	
			}		
			]
		});

    });


var result=[];	 //存放原始数据
var result2=[];	 //存放处理的数据
//获取条件
function add(obj){
	var flag=false;	
	for(var i=0;i<result.length;i++)
	{			
		if(result[i]==obj){
			flag = true;
		}		
	}
	if(!flag){	          
		result.push(obj);		
	}
	var temp = chuli(result);  //数据处理展示
	$("#chooseResult").html("");
	$("#chooseResult").append(temp);
} 

function chuli(obj){
	var temp=[];
	for(var i=0;i<obj.length;i++)
	{			
	   var ctt = "<a role='button' onclick='del(text)'>"+obj[i]+" <font color='#FF0000'>X</font></a>&nbsp;&nbsp;&nbsp;&nbsp;";
	   temp.push(ctt);
	}
	return temp;
}

//删除条件
function del(obj){	
	var ss = obj.substring(0,obj.length-2);	
	removeByValue(result,ss);	
	var temp = chuli(result);  //数据处理展示
	$("#chooseResult").html("");
	$("#chooseResult").append(temp);
	
}

//删除数组数据
function removeByValue(arr, val) {
  for(var i=0; i<arr.length; i++) {
	if(arr[i] == val) {
	  arr.splice(i, 1);
	  break;
	}
  }
}


//查询实时监测详情
$("#xq_search").on("click", function() {  
     alert("请选择查询条件！");
	 
});

//查询实时车辆监测详情
//关闭当前页面时，清除掉此次操作内容




// 选择查询条件事件
/* var names = "";
function getQueryCriteria(){
	
	$.ajax(function(){
		
		success:function(data){
			var htmlName = "";
			$.each(data, function(k,v){
				var name = data[k].name;
				htmlName += <a href="add(name)">name</a> + "  ";
			})
			
		}
	})
	
	
}*/

//车辆信息监测(首页)柱状图
function initBarChart(){
	$('#barChart_load').highcharts({
        chart: {
            type: 'column'
        },
		title: {
            text:''
        },
        xAxis: {
            categories: ['负载车辆', '空载车辆']
        },
		yAxis: {
            min: 0,
            title: {
                text: ''
            }
        },
		plotOptions: {
        column: {
            pointWidth:25 
        }
        },
		credits: { enabled:false},  //去掉水印
		legend: { enabled: false },  //去掉图例
		tooltip: {
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0"></td>' +
            '<td style="padding:0"><b>{point.y} 辆</b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        series: [{
            data: [{				
                name: '负载数量',
                color: '#99d07c',
                y: 539
            }, {				
                name: '空载数量',
                color: '#039b9e',
                y: 61
            }]
        }]
    });
	
	$('#barChart_contact').highcharts({
        chart: {
            type: 'column'
        },
		title: {
            text:''
        },
        xAxis: {
            categories: ['在线车辆', '离线车辆']
        },
		yAxis: {
            min: 0,
            title: {
                text: ''
            }
        },
		plotOptions: {
        column: {
            pointWidth:25 
        }
        },
		credits: { enabled:false},  //去掉水印
		legend: { enabled: false },  //去掉图例
		tooltip: {
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0"></td>' +
            '<td style="padding:0"><b>{point.y} 辆</b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        series: [{
            data: [{				
                name: '在线数量',
                color: '#2ec7c9',
                y: 539
            }, {				
                name: '离线数量',
                color: '#f4bc8e',
                y: 61
            }]
        }]
    }); 
	
	$('#barChart_charge').highcharts({
        chart: {
            type: 'column'
        },
		title: {
            text:''
        },
        xAxis: {
            categories: ['充电车辆', '未充电车辆']
        },
		yAxis: {
            min: 0,
            title: {
                text: ''
            }
        },
		plotOptions: {
        column: {
            pointWidth:25 
        }
        },
		credits: { enabled:false},  //去掉水印
		legend: { enabled: false },  //去掉图例
		tooltip: {
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0"></td>' +
            '<td style="padding:0"><b>{point.y} 辆</b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        series: [{
            data: [{				
                name: '充电数量',
                color: '#3aced2',
                y: 198
            }, {				
                name: '未充电数量',
                color: '#b6a2de',
                y: 402
            }]
        }]
    }); 	
}
	
function initPieChart(){
	 $('#pieChart_aram').highcharts({
        chart: {		
			ckgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: ''
        },
        tooltip: {
            headerFormat: '{series.name}<br>',
            pointFormat: '{point.name}: <b>{point.percentage:.1f}%</b>'
        },
		credits: { enabled:false},  //去掉水印
        plotOptions: {
            pie: {
                 allowPointSelect: true,
                 cursor: 'pointer',	
				 dataLabels: {  
                 enabled: true,  
                 useHTML: true, // 一定要加上  
                 formatter : function() {  
                  "<span style='white-space:pre'>    </span> return <p style='width: 100px; display:inline-block; white-space:pre-wrap;'><b>"+this.point.name+"</b>: "+this.percentage.toFixed(2)+" %</p>"; // 重点在white-space:pre-wrap  
                                     }  					
            } 			
            }
            
        },
        series: [{
            type: 'pie',
            name: '告警类型',
            data: [		             
					{
						name: '电子围栏告警',
						color:'#60c0dd',
						y: 300          
					},
					 {
						name: '温湿度告警',
						color:'#9bca63',
						y: 50          
					},
					{
						name: '车体告警',
						color:'#fe8463',
						y: 100          
					},
					 {
						name: '通信状态告警',
						color:'#fad860',
						y: 40          
					},
					 {
						name: '终端故障告警',
						color:'#f3a43b',
						y: 120          
					}     
            ]
        }]
    });
}

function temps(path){
    window.location=path;
}

//tree event
$('#using_json').on("changed.jstree", function (e, data) {
   //debugger;
   //树结构中获取车牌号numberPlate
   //alert(data.selected);
   //alert(data.node.original.numberPlate);
   // console.log(data.selected);
   
   
});